home *** CD-ROM | disk | FTP | other *** search
- {
- This unit contains an object which creates a modeless "status" dialog box.
- The dialog box displays a percentage complete and a comment. It allows the
- user to cancel the current task. It can be used in a DLL if so desired.
- One consideration, if it is used in a DLL is that I have been unsuccessful
- in my attempts to allow more than one instance of this object to be used
- at once. (I may fix this in the future, but for my current applications,
- this is not a problem.)
-
- June 18/93
- Bradley Plett
- CIS ID: 71075,2010
- }
- UNIT statobj;
-
- INTERFACE
-
- USES
- strings, wintypes, winprocs, windos;
-
- {$R status.res}
-
- TYPE
- stat_dlg = OBJECT
- FUNCTION init(inhwnd : hwnd) : bool;
- FUNCTION cancelled : boolean;
- PROCEDURE update(inperc : REAL; incmt : pchar);
- PROCEDURE done;
- PRIVATE
- {parent window}
- parent_hwnd : hwnd;
- {message handling procedure}
- msg_proc : tFarProc;
- END; {stat_dlg}
-
- IMPLEMENTATION
-
- CONST
- BWCCInst : tHandle = tHandle(0);
- BorButton : pchar = 'BorBtn';
- {dialog box handle}
- stdlg_hwnd : hwnd = 0;
-
- VAR
- {variable indicating the user's desire to cancel the task}
- user_abort : boolean;
-
- FUNCTION LoadBWCC : Bool;
- {
- Check to see if BWCC is available.
- }
- VAR
- aWndClass: tWndClass;
- prevMode: word;
- BEGIN {LoadBWCC}
- IF (BWCCInst = 0) THEN
- BEGIN
- prevMode:= SetErrorMode($8000); {SEM_NoOpenFileErrorBox}
- BWCCInst:= LoadLibrary('BWCC.DLL');
- SetErrorMode(prevMode);
- IF (BWCCInst < 32) THEN
- BEGIN
- LoadBWCC:= False;
- BWCCInst:= 0;
- Exit
- END;
- END;
- LoadBWCC:= GetClassInfo(System.hInstance,BorButton,aWndClass)
- END; {LoadBWCC}
-
- PROCEDURE UnLoadBWCC;
- {
- Unload BWCC if it's loaded.
- }
- BEGIN {UnLoadBWCC}
- IF (BWCCInst <> 0) THEN
- BEGIN
- FreeLibrary(BWCCInst);
- BWCCInst:= 0
- END;
- END; {UnLoadBWCC}
-
- FUNCTION stat_dlg_proc(Dlg : hWnd; msg : word;
- wParam : word; lParam : longint) : longint; EXPORT;
- {
- Handle the dialog box messages.
- }
- BEGIN {stat_dlg_proc}
- stat_dlg_proc := 0;
- CASE msg OF
- wm_InitDialog :
- BEGIN
- {get rid of the "Close" menu option}
- DeleteMenu(GetSystemMenu(dlg, false), sc_close, mf_bycommand);
- stat_dlg_proc := -1;
- END; {wm_InitDialog}
- wm_Command : BEGIN
- {if the user selected "Cancel", set the global variable}
- IF (wParam = id_Cancel) THEN
- user_abort := true;
- END; {wm_Command}
- END; {case}
- END; {stat_dlg_proc}
-
- FUNCTION stat_dlg.init(inhwnd : hwnd) : bool;
- {
- Initialize the object, especially its variables, and try to create the
- dialog box.
- }
-
- BEGIN {stat_dlg.init}
-
- {initialize variables}
- init := false;
- IF (stdlg_hwnd <> 0) THEN
- exit;
- user_abort := false;
- parent_hwnd := inhwnd;
- msg_proc := NIL;
-
- {set up message handling procedure}
- msg_proc := MakeProcInstance(@stat_dlg_proc, hInstance);
- IF (msg_proc = NIL) THEN
- messagebox(0, 'Problems making procedure instance', 'Error',
- mb_iconstop + mb_ok)
- ELSE
- BEGIN
-
- {create the dialog box, using Borland Custom Controls if available}
- IF LoadBWCC THEN
- stdlg_hwnd := CreateDialog(hInstance, 'StatusB',
- parent_hwnd, msg_proc)
- ELSE
- stdlg_hwnd := CreateDialog(hInstance, 'Status',
- parent_hwnd, msg_proc);
- IF (stdlg_hwnd = 0) THEN
- BEGIN
- messagebox(0, 'Could not create a window', 'Error',
- mb_iconstop + mb_ok);
- {clean up}
- done;
- END
- ELSE
- BEGIN
- {display the dialog box and disable the parent window}
- ShowWindow(stdlg_hwnd, sw_show);
- EnableWindow(parent_hwnd, false);
- init := true;
- END; {else}
- END; {else}
-
- END; {stat_dlg.init}
-
- FUNCTION stat_dlg.cancelled : boolean;
- {
- Check Windows messages and check if user selected "cancel".
- }
- VAR
- message : tmsg; {messages to process}
- BEGIN {stat_dlg.cancelled}
- {check if there's a message waiting}
- WHILE PeekMessage(Message, 0, 0, 0, pm_remove) DO
- BEGIN
- {handle messages for the dialog box and pass on others}
- IF (stdlg_hwnd = 0) OR (NOT isdialogmessage(stdlg_hwnd, message)) THEN
- BEGIN
- TranslateMessage(Message);
- DispatchMessage(Message);
- END; {if}
- END;
- cancelled := user_abort;
- END; {stat_dlg.cancelled}
-
- PROCEDURE stat_dlg.update(inperc : REAL; incmt : pchar);
- {
- Display the percentage as a bar, the percentage as text, and a comment.
- }
-
- CONST
- rgb_Blue = $00FF0000; {blue rectangle interior}
- id_prect = 101; {rectangle id}
- id_ptext = 102; {% text id}
- id_pcnt = 103; {count id}
-
- VAR
- dc : hdc; {device context}
- tmps : ARRAY [0..20] OF char; {string for output}
- NewBrush,
- OldBrush: HBrush; {brushes used for rectangle interior}
- NewPen ,
- OldPen : HPen; {pens used for rectangle outline}
- stat_rect : trect; {rectangle for % bar}
- rect_right : integer; {right coordinate of bar}
- rect_hwnd : hwnd; {rectangle window handle}
- tmp_point : tpoint; {used for coordinate conversion}
-
- BEGIN {stat_dlg.update}
-
- {only update display if we have a valid window}
- IF (stdlg_hwnd <> 0) THEN
- BEGIN
-
- {get device context}
- dc := getdc(stdlg_hwnd);
-
- {get rectangle's handle}
- rect_hwnd := GetDlgItem(stdlg_hwnd, id_prect);
-
- {get rectangle's coordinates}
- GetwindowRect(rect_hwnd, stat_rect);
-
- {convert coordinates to be relative to the dialog box}
- WITH stat_rect DO
- BEGIN
- tmp_point.x := left;
- tmp_point.y := top;
- ScreenToClient(stdlg_hwnd, tmp_point);
- left := tmp_point.x + 1;
- top := tmp_point.y + 1;
- tmp_point.x := right;
- tmp_point.y := bottom;
- ScreenToClient(stdlg_hwnd, tmp_point);
- right := tmp_point.x - 1;
- bottom := tmp_point.y - 1;
- END; {with}
-
- {save old brush and select a new one for painting the bar}
- NewBrush := CreateSolidBrush(rgb_blue);
- IF (NewBrush = 0) THEN { Use the new brush if it was made }
- OldBrush := 0
- ELSE
- OldBrush := SelectObject(DC, NewBrush);
-
- {save old pen and select a new one for the bar's outline}
- NewPen := CreatePen(ps_Solid, 1, rgb_blue);
- IF (NewPen = 0) THEN
- OldPen := 0
- ELSE
- OldPen := SelectObject(DC, NewPen);
-
- {calculate the bar's right coordinate}
- rect_right := round(inperc / 100.0 *
- (stat_rect.right - stat_rect.left) +
- stat_rect.left);
-
- {draw the bar}
- rectangle(dc, stat_rect.left, stat_rect.top,
- rect_right, stat_rect.bottom);
-
- {restore the old brush and pen}
- IF (OldBrush <> 0) THEN { Restore the original brush now! }
- BEGIN
- SelectObject(DC, OldBrush);
- DeleteObject(NewBrush)
- END; {if}
- IF (OldPen <> 0) THEN
- BEGIN
- SelectObject(DC, OldPen);
- DeleteObject(NewPen)
- END; {if}
-
- {display the % complete in text form}
- str(inperc:1:0, tmps);
- strcat(tmps, '%');
- SetDlgItemText(stdlg_hwnd, id_ptext, tmps);
-
- {display the comment}
- SetDlgItemText(stdlg_hwnd, id_pcnt, incmt);
-
- {release the device context}
- releasedc(stdlg_hwnd, dc);
-
- END; {if}
-
- END; {stat_dlg.update}
-
- PROCEDURE stat_dlg.done;
- {
- Clean up the object.
- }
- var tmps : array [0..20] of char; {?????}
- BEGIN {stat_dlg.done}
-
- {enable the parent window and get rid of the dialog box}
- EnableWindow(parent_hwnd, true);
- IF (stdlg_hwnd <> 0) THEN
- DestroyWindow(stdlg_hwnd);
- {reset so it can be used again}
- stdlg_hwnd := 0;
-
- {get rid of the message handling procedure's handle}
- IF (msg_proc <> NIL) THEN
- FreeProcInstance(msg_proc);
-
- {unload Borland Custom Control library}
- UnLoadBWCC;
-
- END; {stat_dlg.done}
-
- BEGIN {statobj}
- END. {statobj}